package hello;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


import java.util.*;
import java.lang.*;
import com.alibaba.fastjson.*;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import com.mysql.cj.jdbc.*;



class TestPerson {
    private int age;
    private String name;

    private  int i;

    public TestPerson(){

    }

    public TestPerson(int age,String name){
        this.age=age;
        this.name=name;
    }

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;

        System.out.println(i);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

class RunnableDemo implements Runnable {
    private Thread t;
    private String threadName;

    RunnableDemo( String name) {
        threadName = name;
        System.out.println("Creating " +  threadName );
    }

    public void run() {
        System.out.println("Running " +  threadName );
        try {
            for(int i = 4; i > 0; i--) {
                System.out.println("Thread: " + threadName + ", " + i);
                // 让线程睡眠一会
                Thread.sleep(50);
            }
        }catch (InterruptedException e) {
            System.out.println("Thread " +  threadName + " interrupted.");
        }
        System.out.println("Thread " +  threadName + " exiting.");
    }

    public void start () {
        System.out.println("Starting " +  threadName );
        if (t == null) {
            t = new Thread (this, threadName);
            t.start ();
        }
    }
}

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        //SpringApplication.run(Application.class, args);
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "value1");
        map.put("2", "value2");
        map.put("3", "value3");

        System.out.println("hello,world,lbx");

        int i = 0;
        System.out.println(i);

//        testjson();
//        testvector();
//        testMap();

        testMysql();

        RunnableDemo R1 = new RunnableDemo( "Thread-1");
        R1.start();

        RunnableDemo R2 = new RunnableDemo( "Thread-2");
        R2.start();

        char c;
        // 使用 System.in 创建 BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入字符, 按下 'q' 键退出。");
        // 读取字符
        do {
            try{
                c = (char) br.read();
                System.out.println(c);
            }catch (Exception e)
            {
                break;
            }
        } while (c != 'q');
    }


    public  static void  testMap()
    {
        Map m1 = new HashMap();
        m1.put("Zara", "8");
        m1.put("Mahnaz", "31");
        m1.put("Ayan", "12");
        m1.put("Daisy", "14");

        for (Object key : m1.keySet()) {
            System.out.println(key + " ：" + m1.get(key));
        }

        System.out.println();
        System.out.println(" Map Elements");
        System.out.print("\t" + m1 + "\n");

        JSONObject json =new JSONObject(m1);

        System.out.println(json);
    }
    public static void testjson()
    {
        TestPerson json = new TestPerson(19,"李明");
        List<TestPerson> list = new ArrayList<TestPerson>();
        list.add(json);
        list.add(new TestPerson(12,"张三"));
        //将集合或者对象序例化成JSON
        System.out.println(JSON.toJSON(json));
        System.out.println( JSON.toJSON(list) );
        //Json串反序列化成对象
        TestPerson person = JSON.parseObject("{\"name\":\"李明\",\"age\":19}", TestPerson.class);
        System.out.printf("name:%s,age:%d\n",person.getName(),person.getAge());

        String str = "[{\"name\":\"李明\",\"age\":19},{\"name\":\"张三\",\"age\":12}]";
        //数组对象反序列化成集合
        List<TestPerson> listPerson = JSON.parseArray(str,TestPerson.class);

        for(TestPerson item : listPerson){
            System.out.println( item.getName() );
            System.out.println( item.getAge() );
        }

        //没有对象直接解析JSON对象
        JSONObject jobj = JSON.parseObject("{\"name\":\"李明\",\"age\":19}");
        System.out.printf("name:%s,age:%d\n",jobj.getString("name"),jobj.getBigInteger("age"));

        //没有对象直接解析JSON数组
        JSONArray jarr = JSON.parseArray("[{\"name\":\"李明\",\"age\":19},{\"name\":\"张三\",\"age\":12}]");

        for(int i=0,len=jarr.size();i<len;i++){
            JSONObject temp=  jarr.getJSONObject(i);
            System.out.printf("name:%s,age:%d\n",temp.getString("name"),temp.getBigInteger("age"));
        }

        for(Object obj:jarr){
            System.out.println(obj.toString());
        }
    }



    public static void testvector()
    {
        Vector v = new Vector(3, 1);
        System.out.println("Initial size: " + v.size());
        System.out.println("Initial capacity: " +
                v.capacity());


        v.addElement(new Integer(2));
        v.addElement(new Integer(1));

        v.addElement(new Integer(2));
        v.addElement(new Integer(3));
        v.addElement(new Integer(4));
        System.out.println("Capacity after four additions: " +
                v.capacity());

        v.addElement(new Double(5.45));
        System.out.println("Current capacity: " +
                v.capacity());
        v.addElement(new Double(6.08));
        v.addElement(new Integer(7));
        System.out.println("Current capacity: " +
                v.capacity());
        v.addElement(new Float(9.4));
        v.addElement(new Integer(10));
        System.out.println("Current capacity: " +
                v.capacity());
        v.addElement(new Integer(11));
        v.addElement(new Integer(12));
        System.out.println("First element: " +
                (Integer)v.firstElement());
        System.out.println("Last element: " +
                (Integer)v.lastElement());
        if(v.contains(new Integer(3)))
            System.out.println("Vector contains 3.");
        // enumerate the elements in the vector.
        Enumeration vEnum = v.elements();
        System.out.println("\nElements in vector:");
        while(vEnum.hasMoreElements())
            System.out.print(vEnum.nextElement() + " ");
        System.out.println();
    }

    public static void testMysql()
    {
        String driveName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/mysql";
        String user = "root";
        String pass = "65386538";

        try {
            Class.forName(driveName);

            // 连接方法一
            Connection con = DriverManager.getConnection(url, user, pass);

            // 连接方法二
            //String URL = "jdbc:mysql://127.0.0.1:3306/student?user=root&password=123456&serverTimezone=GMT%2B8";
            //Connection con = DriverManager.getConnection(URL);

            Statement state = con.createStatement();
            String querySql = "select * from db";
            ResultSet result = state.executeQuery(querySql);

            while (result.next()) {
                System.out.println("学号：" + result.getString("User"));

                System.out.println();
            }

            con.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }



//         String DB_URL = "jdbc:mysql://localhost:3306/manage?useSSL=false&serverTimezone=UTC";
//        Connection conn conn = DriverManager.getConnection(DB_URL,user,pass);
//
////        Class.forName("com.mysql.jdbc.Driver");
////        在 IDEA 里面提示是: Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary
////        意思是说原本的驱动器不赞成 或者 是废弃了，自动换成了新的驱动器 com.mysql.cj.jdbc.Driver
//        Class.forName("com.mysql.cj.jdbc.Driver");


    }


}


